﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Entity;

namespace BLL
{
    public class RoleVo
    {
        public string RoleId { get; set; }
        public string RoleName { get; set; }
        public int RoleType { get; set; }
        public List<ModuleVo> RoleModules { get; set; }
        private ModuleDao moduleDao = new ModuleDao();
        private PrivilegesDAL privilegesDAL = new PrivilegesDAL();

        public RoleVo()
        {
            RoleId = "";
            RoleName = "";
            RoleModules = new List<ModuleVo>();
        }

        public List<ModuleVo> getAuthorizedModules()
        {
            RoleModules = new List<ModuleVo>();
            List<BoModules> mList = moduleDao.GetRoleModules(RoleId);
            foreach (BoModules m in mList)
            {
                ModuleVo child = new ModuleVo();
                child.ModuleID = m.MOD_ID;
                child.MOduleName = m.MOD_NAME;
                child.DisplayName = m.MOD_DISPLAYNAME;
                child.ModuleURL = m.MOD_URL;
                RoleModules.Add(child);
            }
            return RoleModules;
        }

        public void SaveRoleModules()
        {
            privilegesDAL.ClearPrivileges(RoleId);
            foreach (ModuleVo mv in RoleModules)
            {
                BoPrivileges pri = new BoPrivileges();
                pri.PRI_ROLES = RoleId;
                pri.PRI_MODULES = mv.ModuleID;
                privilegesDAL.Insert(pri);
            }
        }
    }
}
